package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.UserStoresDao;
import com.suyang.syt.model.UserStores;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.UserStoresDTO;
import com.suyang.syt.service.interfaces.UserStoresService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Service
public class UserStoresServiceImpl implements UserStoresService {

	@Autowired
	private UserStoresDao userStoresDao;

	public PagerEasyUI<UserStoresDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, String checkName, String checkStatus, String userid) throws Exception {
		String ids[] = userid.split(",");
		StringBuilder hql = new StringBuilder(" from UserStores us where 1=1  ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and us.createTime>='").append(createDate1).append("'");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and us.createTime<='").append(createDate2).append("'");
		}
		if (EmptyUtil.isNotEmpty(checkName)) {
			hql.append(" and us.name like '%").append(checkName).append("%'");
		}
		if (EmptyUtil.isNotEmpty(userid)) {
			hql.append(" and us.userid = ").append(Long.parseLong(ids[0]));
		}
		if (EmptyUtil.isNotEmpty(checkStatus)) {
			hql.append(" and us.status='").append(checkStatus).append("'");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by us.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<UserStoresDTO> easyUIPager = new PagerEasyUI<UserStoresDTO>();
		List<UserStores> userStores = userStoresDao.findByEasyUI(hql.toString(), page, rows);
		List<UserStoresDTO> userStoresDTOList = new ArrayList<UserStoresDTO>();
		UserStoresDTO userStoresDTO = null;
		for (UserStores us : userStores) {
			userStoresDTO = new UserStoresDTO(us);
			userStoresDTOList.add(userStoresDTO);
		}
		easyUIPager.setRows(userStoresDTOList);
		easyUIPager.setTotal((Long) userStoresDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj deleteStores(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			userStoresDao.deleteByIds(ids);
			return new AjaxObj(1, "删除收藏成功");
		}
		return new AjaxObj(0, "请选择要删除的收藏");
	}

	@Override
	public AjaxObj getobj(long id, long status) throws Exception {
		return new AjaxObj(0, StringUtil.ERROR_MSG);
	}

	@Override
	public List<UserStores> list(String ids) throws Exception {
		String hql = " from UserStores us where us.id in(" + ids + ")";
		return userStoresDao.list(hql);
	}

}
